package org.dandsoft.blackbox.laboratory.tools.view;

import org.dandsoft.blackbox.laboratory.tools.JCRMonitorStorage;
import org.dandsoft.blackbox.snn.Signal;
import org.dandsoft.blackbox.snn.ObjectId;
import org.dandsoft.blackbox.snn.Time;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.awt.image.BufferedImage;
import java.awt.*;

/**
 * Created by IntelliJ IDEA.
 * User: dvilyuzhanin
 * Date: 30.03.2009 17:01:00
 */
public class GifDiagrame {


    public static void main(String[] args) throws Exception {
        makeScaleFiel();
        File file = new File("e:/lighter");
        final float maxValue = 1;
        if (!file.exists()) {
            throw new RuntimeException("repository not found");
        }
        JCRMonitorStorage monitor = JCRMonitorStorage.createRepository(file.getAbsolutePath());

        long times = monitor.getTimesCount();
        int ids = monitor.getMaxObjectId();


        BufferedImage buffer = new BufferedImage(ids, (int) times, BufferedImage.TYPE_INT_RGB);
        Graphics g = buffer.getGraphics();
        g.setColor(Color.white);
        g.fillRect(0, 0, buffer.getWidth(), buffer.getHeight());
        for (long time = 0; time < times; time++) {
            Map<ObjectId, Signal> signals = monitor.getEnviroment(new Time(time));
            if (signals != null) {
                //System.out.println(signals);
                for (Map.Entry<ObjectId, Signal> entry : signals.entrySet()) {
                    float value = entry.getValue().getValue().getValue() / maxValue;
                    int index = (int) Math.floor(value * ValueColorSpec.SCALE_SIZE);
                    g.setColor(ValueColorSpec.getColor(index - 1));
                    g.fillRect(entry.getKey().getValue(), (int) time, 1, 1);
                }
            }
        }

        ImageIO.write(buffer, "bmp", new File(file.getAbsolutePath() + ".bmp"));
    }

    public static void makeScaleFiel() throws IOException {
        BufferedImage image = new BufferedImage(ValueColorSpec.SCALE_SIZE, 100, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        for (int i = 0; i < ValueColorSpec.SCALE_SIZE; i++) {
            g.setColor(ValueColorSpec.getColor(i));
            g.fillRect(i, 0, 1, 100);
        }
        ImageIO.write(image, "bmp", new File("e:/scale.bmp"));
    }
}
